class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        // 找复写数
        int cur = 0, dest = -1;
        while (cur <= arr.size())
        {
            if (arr[cur])
            {
                dest++;
            }
            else
            {
                dest += 2;
            }

            if (dest < arr.size() - 1)
                cur++;
            else
                break;
        }

        // 复写0
        while (cur >= 0)
        {
            // 处理特殊情况
            if (dest == arr.size())
            {
                arr[--dest] = arr[cur--];
                --dest;
            }

            if (arr[cur])
            {
                arr[dest--] = arr[cur--];
            }
            else
            {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }
    }
};